<?php
/**
* Class client Script quản lý script trên client
*/
class ClientScript{
	public static function addContactToGroup($groupId){
		$linkAddToGroup = Yii::app()->createUrl('groups/contacts/addtogroup/id/'.$groupId);
		$script = <<<EOD
jQuery(document).ready(function(a) {
	$('#add_to_group').click(function(e){
		$('.loading').show();
		$(this).prop('disabled', true);
		var list_contact_id = [];
		$("#contact-grid :checked").each(function(){
			list_contact_id.push($(this).val());
		});
		if(list_contact_id.length > 0){
			$.ajax({
				type: 'POST',
				url: '$linkAddToGroup',
				data: 'contacts=' + list_contact_id,
				dataType:'json',
				complete: function(){
					$('.loading').hide();
					$('#add_to_group').prop('disabled', false);	
				},
				success: function(data){
					if(data.success == true){
						window.location.reload();
					}
				}
			});
		}
	});
});
EOD;
		return $script;
	}

	/**
	 * Script to auto expand menu left sidebar 
	 */	
	public static function autoExpandMenuParent(){
		$script = <<<EOD
jQuery(document).ready(function(a) {
	$('.active').parent().show();
});
EOD;
		return $script;
	}	

	/**
	 * Script to change campaign type in step one create campaign
	 */
	public static function ChangeCampaignType(){
		$script = <<<EOD
jQuery(document).ready(function(a) {
	jQuery('.type-campaign-item').click(function(e){
		var type = $(this).attr('data-type').trim();
		if($(this).attr('data-comingsoon') == 1) {
			alert('Chức năng đang xây dựng!');return;
		}
		var pattern = /[^\s]/g;
		if(!pattern.test(type)) return;
		$('.type-campaign-item').removeClass('active');
		$(this).addClass('active');
		$('#CampaignStepOneForm_type').val(type);
	});
});
EOD;
		return $script;
	}

	/**
	 * Script to make auto complete 
	 */
	public static function autosuggestContactsCampaign($data, $id){
		$script = <<<EOD
jQuery(document).ready(function(a) {
		    jQuery('#$id').autoSuggest("$data", {
		    	selectedItemProp: "name", 
		    	searchObjProps: "name",
		    	usePlaceholder:"true",
		    	startText: "nhập email hoặc tên liên hệ",
		    	emptyText: "Không có kết quả phù hợp",
		    	asHtmlID: "$id",
		    	minChars:2
		    });		
});
EOD;
		return $script;
	}

    public static function profileScript() {
        $script = <<<EOD
jQuery(document).ready(function(a) {
		    jQuery('#UserProfile_avatar').live("change", function() {
                    if (this.files && this.files[0]) {
                        var reader = new FileReader();
                        reader.onload = function (e) {
                            $('#img_avatar').attr('src', e.target.result);
                        };
                        reader.readAsDataURL(this.files[0]);
                      }
                    });
                     jQuery('button[type="reset"]').live('click', function(event){
                        // executes before the form has been reset

                        event.preventDefault();
                        // stops the form from resetting after this function

                        $(this).closest('form').get(0).reset();
                        // resets the form before continuing the function

                        // executes after the form has been reset
                        $('#img_avatar').attr('src', img);
                        });
                        $('#UserProfile_full_name').focus();
});
EOD;
        return $script;
    }


}
?>